package com.tonyodev.fetch2fileserver.database;

import ohos.utils.zson.ZSONObject;

import com.tonyodev.fetch2core.Extras;
import com.tonyodev.fetch2core.FileResource;

import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;

public class FileResourceExtensionsKt {

    public static final FileResourceInfo toFileResourceInfo(FileResource fileResource) {
        FileResourceInfo fileResourceInfo = new FileResourceInfo();
        fileResourceInfo.setId(fileResource.getId());
        fileResourceInfo.setFile(fileResource.getFile());
        fileResourceInfo.setLength(fileResource.getLength());
        fileResourceInfo.setName(fileResource.getName());
        fileResourceInfo.setMd5(fileResource.getMd5());
        fileResourceInfo.setExtras(fileResource.getExtras().toJSONString());
        return fileResourceInfo;
    }


    public static final FileResource toFileResource(FileResourceInfo fileResourceInfo) {
        FileResource fileResource = new FileResource();
        fileResource.setId(fileResourceInfo.getId());
        fileResource.setFile(fileResourceInfo.getFile());
        fileResource.setLength(fileResourceInfo.getLength());
        fileResource.setName(fileResourceInfo.getName());
        fileResource.setMd5(fileResourceInfo.getMd5());
        Extras extras;
        try {
            boolean var3 = false;
            Map<String, String> map = new LinkedHashMap<>();
            ZSONObject josn = ZSONObject.stringToZSON(fileResourceInfo.getExtras());
            for (String key : josn.keySet()) {
                map.put(key, josn.getString(key));
            }
            extras = new Extras(map);
        } catch (Exception e) {
            extras = Extras.getEmptyExtras();
        }
        fileResource.setExtras(extras);
        return fileResource;
    }
}
